<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2018/4/24  9:58
 */
namespace app\admin\Controller;
use think\Controller;

class Shool extends Common
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        if(request()->isPost())
        {
            //修改
            $data['title'] = input('title');
            $data['content'] = input('content');
            if($data['title'] != null || $data['content'] != null)
            {
                $file=request()->file('image');
                if($file)
                {
                    $info = $file->move(ROOT_PATH . 'public' . DS . 'static' . DS . 'uploads');
                    if($info)
                    {
                        $data['img']= $info->getSaveName();
                    }
                }
                $res = db('shool')->where('id', 1)->update($data);
                if($res)
                {
                    $this->success('操作成功', url('shool/index'));
                }else{
                    $this->success('操作失败');
                }

            }
        }
        $data = db('shool')->where('id', 1)->find();
//        halt($data);
        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     * 讲师列表
     */
    public function list_teach()
    {
        $data = db('shool')->where('id', 'not in', 1)->select();
        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     *添加表单
     */
    public function add_teach()
    {
        return $this->fetch();
    }

    /**
     * 编辑、添加
     */
    public function edit_teach()
    {
        $id = input('id');
        $shool = db('shool');
        if(request()->isPost())
        {
            $data['title'] = input('title');
            $data['content'] = input('content');
            $file=request()->file('image');
            if($file)
            {
                $info = $file->move(ROOT_PATH . 'public' . DS . 'static' . DS . 'uploads');
                if($info)
                {
                    $data['img']= $info->getSaveName();
                }
            }

            if($id)
            {
//                echo 'aa';exit;
                //修改
                $res = $shool->where('id', $id)->update($data);
//                $sql = $shool->getLastSql();
//                echo $sql;exit;
                if($res){$this->success('操作成功', url('Shool/list_teach'));}else{$this->success('操作失败');}

            }else{
                //添加
                $res = $shool->insert($data);
                if($res){$this->success('操作成功');}else{$this->success('操作失败');}
            }
        }

        $data = $shool->where('id', $id)->find();
        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     * 删除
     */
    public function del_teach(){
        $id = input("id");
//        $this->success($id);
        if(!$id) $this->error('参数有误');
        $da=db('shool')->where('id', $id)->find();
        if(!$da) $this->error('数据不存在');
        $pic = ROOT_PATH . 'public/static/uploads/'.$da['img'];
        unlink($pic);
        $res=db('shool')->where("id='$id'")->delete();
        if(!$res) $this->error('删除失败');
        $this->success("删除成功!",url('shool/list_teach'));
    }
}